 <?php

define("APP_ID","wx***************");
define("APP_SECRET","**************");

define("TPL_ID","**************");


include_once "wx.php";
 
//迭代器
class myIterator implements Iterator 
{
    private $arr;
    public function __construct($arr) 
    {
        $this->arr = $arr;
    }
    function rewind() {         #用于初始化，这个方法无论循环几次只运行一次
       
    }  

    function current() {        #该方法返回值作为value
        return current($this->arr);
    }  

    function key() {             #该方法返回值作为key
        return key($this->arr);
    }  
   
    function next() {           #在每一次循环结束时执行该方法，用于转接下一次循环，可以随便做点什么
        next($this->arr);
        return true;
    }  

    function valid() {            #用于验证，该方法如果返回为false则退出循环
       $vaild = current($this->arr) === false ? false : true;
       return $vaild;
    }  
}

 function formatMsg($openid,$tplid,$valData){
     $data["touser"] = $openid;
     $data["template_id"] = $tplid;
     $data["data"] = formatMsgData($valData);
     return $data;
 }

 function formatMsgData($data){

    $formatData  = [];
    foreach ($data as $key=>$val){
        $formatData[$key]=['value'=>$val];
    }

    return $formatData;
 }

 function http_post_json($url, $data = NULL, $json = false){
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
     if (!empty($data)) {
         if($json && is_array($data)){
             $data = json_encode( $data ,JSON_UNESCAPED_UNICODE);
         }
         curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         if($json){ //发送JSON数据
             curl_setopt($curl, CURLOPT_HEADER, 0);
             curl_setopt($curl, CURLOPT_HTTPHEADER,
                 array(
                     'Content-Type: application/json; charset=utf-8',
                     'Content-Length:' . strlen($data))
             );
         }
     }
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     $res = curl_exec($curl);
     $errorno = curl_errno($curl);
     if ($errorno) {
         return array('errorno' => false, 'errmsg' => $errorno);
     }
     curl_close($curl);
     return json_decode($res, true);
 }